package cn.youframe.frame.ctrl;

import cn.youframe.frame.base.YFBase;
import cn.youframe.frame.beans.Menber;
import cn.youframe.frame.service.LoginService;
import cn.youframe.frame.util.SpringContext;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * User: Administrator
 * DateTime: 13-2-10: 下午10:05
 */
public class SessionListener implements HttpSessionBindingListener,YFBase {
    private UserList ul = UserList.getInstance();
    public Menber getMenber() {
        return menber;
    }

    public void setMenber(Menber menber) {
        this.menber = menber;
    }

    private Menber menber;
    private String key;
    public SessionListener(Menber menber){
        this.menber=menber;
    }
    @Override
    public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) {
        logger.info("用户{}登录了,Session ID:{}",menber.getAccount(),httpSessionBindingEvent.getSession().getId());
        ul.addUser(menber.getAccount());
        LoginService loginService= SpringContext.getBean("loginService",LoginService.class);
        loginService.addOnlineUser(menber);//添加在线用户
        key=loginService.userLogin(menber);//记录登录日志
        logger.info("当前在线用户数：{}",ul.getUserCount());
    }

    @Override
    public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) {
        logger.info("用户{}退出了,Session ID:{}",menber.getAccount(),httpSessionBindingEvent.getSession().getId());
        ul.RemoveUser(menber.getAccount());
        LoginService loginService= SpringContext.getBean("loginService",LoginService.class);
        loginService.delOnlineUser(menber.getAccount());
        loginService.userLogout(key);
    }
}
